﻿module InkType.ViewModel.ValueConverter

open System
open System.Windows
open System.Windows.Data


let Create (convert : Func<'a, 'r>) =
    { new IValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v))
        member x.ConvertBack (v, _, p, _) = raise (NotImplementedException ()) }

let CreateP (convert : Func<'a, 'p, 'r>) =
    { new IValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v, unbox p))
        member x.ConvertBack (v, _, p, _) = raise (NotImplementedException ()) }

let CreateB (convert : Func<'a, 'r>, convertBack : Func<'r, 'a>) =
    { new IValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v))
        member x.ConvertBack (v, _, p, _) = box (convertBack.Invoke (unbox v)) }

let CreateBP (convert : Func<'a, 'p, 'r>, convertBack : Func<'r, 'p, 'a>) =
    { new IValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v, unbox p))
        member x.ConvertBack (v, _, p, _) = box (convertBack.Invoke (unbox v, unbox p)) }

let Create2 (convert : Func<_, _, _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1]))
        member x.ConvertBack (v, _, p, _) = raise (NotImplementedException ()) }

let Create2P (convert : Func<_, _, _, _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1], unbox p))
        member x.ConvertBack (v, _, p, _) = raise (NotImplementedException ()) }

let Create2B (convert : Func<_, _, _>, convertBack : Func<_, _ * _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1]))
        member x.ConvertBack (v, _, p, _) = let x, y = convertBack.Invoke (unbox v) in [| x; y |] }

let Create2BP (convert : Func<_, _, _, _>, convertBack : Func<_, _, _ * _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1], unbox p))
        member x.ConvertBack (v, _, p, _) = let x, y = convertBack.Invoke (unbox v, unbox p) in [| x; y |] }

let Create3 (convert : Func<_, _, _, _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1], unbox v.[2]))
        member x.ConvertBack (v, _, p, _) = raise (NotImplementedException ()) }

let Create3P (convert : Func<_, _, _, _, _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1], unbox v.[2], unbox p))
        member x.ConvertBack (v, _, p, _) = raise (NotImplementedException ()) }

let Create3B (convert : Func<_, _, _, _>, convertBack : Func<_, _ * _ * _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1], unbox v.[2]))
        member x.ConvertBack (v, _, p, _) = let x, y, z = convertBack.Invoke (unbox v) in [| x; y; z |] }

let Create3BP (convert : Func<_, _, _, _, _>, convertBack : Func<_, _, _ * _ * _>) =
    { new IMultiValueConverter with
        member x.Convert (v, _, p, _) = box (convert.Invoke (unbox v.[0], unbox v.[1], unbox v.[2], unbox p))
        member x.ConvertBack (v, _, p, _) = let x, y, z = convertBack.Invoke (unbox v, unbox p) in [| x; y; z |] }

